﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei.UPC
{
    public partial class ShowUPCData : UIForm
    {
        public ShowUPCData()
        {
            InitializeComponent();
            ShowUPCData_();
        }
        int ynum = 1;
        int qnum = 15;
        int count = 0;
        int znum = 0;
        public async void ShowUPCData_()
        {
            //http://localhost:5159/api/TSW/GetUPCData
            var res = await HttpClientHelper.RequestUrl<ShowUPCDataDTO, ApiResult<List<ShowUPCDataDTO>>>(URLCommon.Url2, "/TSW/GetUPCData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                count = res.Data.Count();
                znum = Convert.ToInt32(Math.Ceiling((double)count / qnum));
                var data = res.Data.OrderBy(x => x.GoodsMinuteId).Skip((ynum - 1) * qnum).Take(qnum).ToList();
                foreach (var item in data)
                {
                    this.dataGridView1.Rows.Add(item.GoodsMinuteId, item.MemoryNum, item.CommodityName, item.OrderNum, item.GoodsGradeId == 1 ? "一级" : (item.GoodsGradeId == 2 ? "二级" : (item.GoodsGradeId == 3 ? "三级" : "等级")), item.UPCDataName);
                }
                this.Zynum.Text = znum.ToString();
                this.Zcount.Text=count.ToString();
            }
        }
        public class ShowUPCDataDTO
        {
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 助记码
            /// </summary>
            public string MemoryNum { get; set; } = string.Empty;
            /// <summary>
            /// 商品分类Id
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 顺序号 
            /// </summary>
            public int OrderNum { get; set; }
            /// <summary>
            /// 等级ID
            /// </summary>
            public int GoodsGradeId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsGoodsMinute { get; set; } = false;
            /// <summary>
            /// 商品分类名称
            /// </summary>
            public string CommodityName { get; set; } = string.Empty;
        }
        //上一页

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (ynum < 2)
            {
                MessageBox.Show("已经是第一页了");
            }
            else
            {
                ynum--;
                this.dataGridView1.Rows.Clear();
                ShowUPCData_();
            }
        }
        //下一页
        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (ynum ==znum)
            {
                MessageBox.Show("已经是最后一页了");
            }
            else
            {
                ynum++;
                this.dataGridView1.Rows.Clear();
                ShowUPCData_();
            }
        }
        //http://localhost:5159/api/TSW/GetUPCData

    }
}
